package cn.caplike.demo.apache.thrift;

import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;
import thrift.generated.Person;
import thrift.generated.PersonService;

/**
 * Description:
 *
 * @author LiKe
 * @version 1.0.0
 * @date 2021-12-06 13:22
 */
public class ThriftClient {

    public static void main(String[] args) throws TTransportException {
        // ~ 以 frame 为单位进行传输, 非阻塞式服务中使用
        TTransport transport = new TFramedTransport(new TSocket("localhost", 8899), 600); // 和服务端协议一致
        TProtocol protocol = new TCompactProtocol(transport); // 和服务端协议一致

        PersonService.Client client = new PersonService.Client(protocol);

        try {
            transport.open(); // 相当于打开 Socket

            Person person = client.getPersonByUsername("张三");
            System.out.println(person.getAge());

            Person person1 = new Person();
            person1.setUsername("李四");
            client.savePerson(person1);
        } catch (Exception any) {
            throw new RuntimeException(any.getMessage(), any);
        } finally {
            transport.close();
        }
    }
}
